package com.fsl.springbootstreamkafka.springkafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
import org.springframework.kafka.listener.MessageListener;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertTrue;

/**
 * @author dell
 * spring 操作kafka进行消息接收
 */
public class SpringKafkaConsumerTest {


    public static void main(String[] args) {

        System.out.println("Start auto");
        ContainerProperties containerProps = new ContainerProperties("mytopic");
        final CountDownLatch latch = new CountDownLatch(200);
        containerProps.setMessageListener(new MessageListener<Integer, String>() {
            @Override
            public void onMessage(ConsumerRecord<Integer, String> message) {
                System.out.println("received------------------>" + message);
                latch.countDown();
            }

        });
        KafkaMessageListenerContainer<String, String> container = SpringKafkaCommon.createContainer(containerProps);
        container.setBeanName("testAuto");
        container.start();
        try{
            assertTrue(latch.await(60, TimeUnit.SECONDS));
        }catch (InterruptedException e){

        }
        container.stop();
        System.out.println("Stop auto");
    }
}
